//////////////////////////////////////////////////////////////////////////////
//       Copyright (c) 2003 - RealTime Gaming.  All rights reserved.        //
//////////////////////////////////////////////////////////////////////////////
//
//  This file contains WINAPI functions that take String objects.
//////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <shlobj.h>

#include "winstring.h"

//////////////////////////////////////////////////////////////////////////////
/// @param hWnd         Window handle to get text from.
/// @param s            String object that will receive text.
/// @oaram nMaxChars    Maximum number of characters to read. (includes NULL)
///
/// Version of windows API GetWindowText() function that takes a String.
//////////////////////////////////////////////////////////////////////////////
int 
GetWindowString( HWND hWnd, String& s, uint32 nMaxChars )
{
    TCHAR* pBuffer = s.getBuffer( nMaxChars );
    int nLen = GetWindowText( hWnd, pBuffer, nMaxChars );
    s.releaseBuffer();

    ASSERT( nLen == (int)s.getLength() );

    return nLen;
}

int 
GetWindowText( HWND hWnd, String& s, uint32 nMaxChars )
{
	return GetWindowString(hWnd, s, nMaxChars);
}

//////////////////////////////////////////////////////////////////////////////
/// @param hWnd         Window handle to get text from.
/// @param s            String object that will receive text.
/// @oaram nMaxChars    Maximum number of characters to read. (includes NULL)
///
/// Version of windows API GetWindowText() function that takes a String.
/// Also, makes sure text is trimmed.
//////////////////////////////////////////////////////////////////////////////
int 
GetWindowStringTrimmed( HWND hWnd, String& s, uint32 nMaxChars )
{
    int nLen = GetWindowString( hWnd, s, nMaxChars );
    s.trim();

    return nLen;
}


//////////////////////////////////////////////////////////////////////////////
/// @param hInstance    Window handle to get text from.
/// @param uID          String ID
/// @param s		    String object that will receive text.
/// @oaram nMaxChars    Maximum number of characters to read. (includes NULL)
///
/// Version of windows API LoadString() function that takes a String.
//////////////////////////////////////////////////////////////////////////////
int 
LoadString(HINSTANCE hInstance, UINT uID, String& s, uint32 nMaxChars)
{
    TCHAR* pBuffer = s.getBuffer( nMaxChars );
    int nLen = ::LoadString(hInstance, uID, pBuffer, nMaxChars );
    s.releaseBuffer();

    ASSERT( nLen == (int)s.getLength() );

    return nLen;
}

UINT 
GetDlgItemText(HWND hDlg, int nIDDlgItem, String& s, int nMaxChars)
{
	TCHAR* pBuffer = s.getBuffer( nMaxChars );
    int nLen = ::GetDlgItemText(hDlg, nIDDlgItem, pBuffer, nMaxChars );
    s.releaseBuffer();

    ASSERT( nLen == (int)s.getLength() );

    return nLen;
}

DWORD 
GetModuleFileName(HMODULE hModule, String& s, DWORD nMaxChars)
{
	TCHAR* pBuffer = s.getBuffer( nMaxChars );
    DWORD nLen = ::GetModuleFileName(hModule, pBuffer, nMaxChars );
    s.releaseBuffer();

    ASSERT( nLen == (int)s.getLength() );

    return nLen;
}

DWORD 
GetModuleFileName(HMODULE hModule, PathName& path, DWORD nMaxChars)
{
	DWORD result;
	String s;

	result = GetModuleFileName(hModule, s, nMaxChars);
	path.set(s);
	return result;
}

int 
MessageBox(HWND hWnd, HINSTANCE hInstance, UINT uIDText, UINT uIDCaption, UINT uType, uint32 nMaxChars)
{
	String text;
	String caption;

	LoadString(hInstance, uIDText, text, nMaxChars);
	LoadString(hInstance, uIDCaption, caption, nMaxChars);

	return MessageBox(hWnd, text, caption, uType);
}

bool8
ShellGetSpecialFolder(int csidl, String& path)
{
	LPITEMIDLIST idl;
	IMalloc *m = 0;
	bool8 result = false;
	TCHAR* szPath = path.getBuffer(MAX_PATH + 1);

	if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, csidl, &idl)))
	{
		/* Convert the path from an "ID List" (whatever that is!) to a path. */
		HRESULT hr = SHGetPathFromIDList(idl, szPath);
		if (SUCCEEDED(hr))
		{
			/* Now we need to free the */
			SHGetMalloc(&m);
			if (m) 
			{
				m->Free(idl);
				m->Release();
			}
			result = true;
		}
		else
		{
			szPath[0] = '\0';
		}
	}
	path.releaseBuffer();

	return result;
}
